﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swcommands;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.swpublished;

using SwAddinHelpers.Contracts;
using SwAddinHelpers.Attributes;
using SwAddinHelpers;
using SwAddinHelpers.Base;
using SwAddinHelpers.Sinks;
using SwAddinHelpersComExtender;
using SwAddinHelpers.ComHelpers;

namespace SwAddinHelpers.TaskPane
{
    public sealed class TaskPaneHost: UserControlHostBase
    {
        private TaskpaneView _TaskpaneView;

        public TaskpaneView TaskpaneView
        {
            get { return _TaskpaneView; }
        }

        public event EventHandler TaskPaneDestroy;
        public event EventHandler TaskPaneDeactivate;
        public event EventHandler TaskPaneActivate;

        private TaskPaneHost(ISwAddinBase owner, string Bitmap, string TaskPaneViewTooltip)
        {
            _OwnerAddin = owner;

            _TaskpaneView = _OwnerAddin.SolidWorksApplication.CreateTaskpaneView2(Bitmap, TaskPaneViewTooltip);
            _TaskpaneView.TaskPaneActivateNotify += new DTaskpaneViewEvents_TaskPaneActivateNotifyEventHandler(_TaskpaneView_TaskPaneActivateNotify);
            _TaskpaneView.TaskPaneDeactivateNotify += new DTaskpaneViewEvents_TaskPaneDeactivateNotifyEventHandler(_TaskpaneView_TaskPaneDeactivateNotify);
            _TaskpaneView.TaskPaneDestroyNotify += new DTaskpaneViewEvents_TaskPaneDestroyNotifyEventHandler(_TaskpaneView_TaskPaneDestroyNotify);
        }

        public TaskPaneHost(ISwAddinBase owner, IExternalUserControl externalControl, string Bitmap, string TaskPaneViewTooltip)
            : this(owner,Bitmap, TaskPaneViewTooltip)
        {
            base.externalControl = externalControl.ExternalUserControl;

            //_GuiControl = (TaskPaneControlBase)
            _TaskpaneView.AddControl(HostedControlTypeProgId, "");
            _GuiControl = (TaskPaneControlBase)_TaskpaneView.GetControl();
            _GuiControl.SetOwner = this;

            //AxControl x = new AxControl(Type.GetTypeFromProgID(HostedControlTypeProgId).GUID.ToString());

            /*
            dynamic d =  ((dynamic)_TaskpaneView.GetControl().GetType().BaseType.BaseType.BaseType.BaseType.BaseType.BaseType.GetProperty("ActiveXInstance",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.FlattenHierarchy).GetValue((Control)_TaskpaneView.GetControl(),null));
            Control o  = (Control)d.GetType().GetField("control", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.FlattenHierarchy).GetValue(d);
            object c = d.GetType().GetField("clientSite", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.FlattenHierarchy).GetValue(d);
            //List<Type> t =  c.GetManagedTypesForSWObject();
            //ComObject co = new ComObject(c);
            */ 
        }

        int _TaskpaneView_TaskPaneDestroyNotify()
        {
            if (TaskPaneDestroy != null) TaskPaneDestroy(null, new EventArgs());
            return 0;
        }

        int _TaskpaneView_TaskPaneDeactivateNotify()
        {
            if (TaskPaneDeactivate != null) TaskPaneDeactivate(null, new EventArgs());
            return 0;
        }

        int _TaskpaneView_TaskPaneActivateNotify()
        {
            if (TaskPaneActivate != null) TaskPaneActivate(null, new EventArgs());
            return 0;
        }

        public override void Teardown()
        {
            try
            {
                base.Teardown();

                _GuiControl.Untie();
                _GuiControl = null;

                _TaskpaneView.TaskPaneActivateNotify -= new DTaskpaneViewEvents_TaskPaneActivateNotifyEventHandler(_TaskpaneView_TaskPaneActivateNotify);
                 _TaskpaneView.TaskPaneDeactivateNotify -= new DTaskpaneViewEvents_TaskPaneDeactivateNotifyEventHandler(_TaskpaneView_TaskPaneDeactivateNotify);
                _TaskpaneView.TaskPaneDestroyNotify -= new DTaskpaneViewEvents_TaskPaneDestroyNotifyEventHandler(_TaskpaneView_TaskPaneDestroyNotify);

                _TaskpaneView.DeleteView();
                Marshal.ReleaseComObject(_TaskpaneView);
                _TaskpaneView = null;
                
            }
            catch (Exception ex)
            { }
        }

    }
}
